package com.ourteam.system.dao.impl;

import com.ourteam.system.dao.ISysResourcePropertyDAO;
import com.ourteam.system.dao.SysResourceProperty;
import com.ourteam.system.dao.SysResourcePropertyDAOQueryBean;

import net.dao.AbstractSpringDAO;
import net.dao.DataAccessObjectHelper;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang.StringUtils;

import org.springframework.jdbc.core.RowMapper;

import java.sql.ResultSet;
import java.sql.SQLException;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * Template Name : DAO服务对象
 * Template Version : 1
 * Template Last Modfiy Date : Fri Nov 17 11:00:11 CST 2017
 * Auto Generate Date : 2018-07-31 15:13:34
  * Warning ! This file will be overwrite when regenerate !
  *
 */
public class AbstractSysResourcePropertyDAOJdbcImpl extends AbstractSpringDAO
    implements ISysResourcePropertyDAO {
    public static final String[] ENTITY_FIELDS = new String[] {
            Id, Remarks, ResourcePropertyCode, ResourcePropertyDefaultValue,
            ResourcePropertyName, ResourcePropertyOrder, ResourcePropertyType,
            ResourcePropertyValue, Status, SysResourceId
        };
    public static final String[] ENTITY_PROPERTIES = new String[] {
            _DataObjectProperty.Id, _DataObjectProperty.Remarks,
            _DataObjectProperty.ResourcePropertyCode,
            _DataObjectProperty.ResourcePropertyDefaultValue,
            _DataObjectProperty.ResourcePropertyName,
            _DataObjectProperty.ResourcePropertyOrder,
            _DataObjectProperty.ResourcePropertyType,
            _DataObjectProperty.ResourcePropertyValue,
            _DataObjectProperty.Status, _DataObjectProperty.SysResourceId
        };
    public static final String[] ENTITY_PK_FIELDS = new String[] { Id };
    public static final String[] ENTITY_AUTO_GEN_PK_FIELDS = new String[] { Id };
    public static final String[] ENTITY_UNIQUE_FIELDS = new String[] {  };
    private static String relatedJoinSQL = null;
    private static String basicFilterSQL = null;
    private DataAccessObjectHelper<SysResourceProperty, SysResourcePropertyDAOQueryBean> daoHelper;
    private com.ourteam.system.dao.impl.AbstractSysResourcePropertyOptionDAOJdbcImpl SysResourcePropertyOptionDao;

    public AbstractSysResourcePropertyDAOJdbcImpl() {
        super();
        daoHelper = new DataAccessObjectHelper<SysResourceProperty, SysResourcePropertyDAOQueryBean>(this);
    }

    protected String getEntityName() {
        return EntityName;
    }

    protected String[] getEntityFields() {
        return ENTITY_FIELDS;
    }

    protected String[] getEntityProperties() {
        return ENTITY_PROPERTIES;
    }

    protected String[] getEntityPKFields() {
        return ENTITY_PK_FIELDS;
    }

    protected String[] getEntityAutoGenPKFields() {
        return ENTITY_AUTO_GEN_PK_FIELDS;
    }

    protected String[] getEntityUniqueFields() {
        return ENTITY_UNIQUE_FIELDS;
    }

    protected String getBaiscQueryFilter() {
        if (basicFilterSQL == null) {
            StringBuffer basicFilterSQLbuffer = new StringBuffer();

            basicFilterSQL = basicFilterSQLbuffer.toString();
        }

        return basicFilterSQL;
    }

    protected String getRelatedJoinSQL() {
        if (relatedJoinSQL == null) {
            StringBuffer relatedJoinSQLBuffer = new StringBuffer();

            relatedJoinSQL = relatedJoinSQLBuffer.toString();
        }

        return relatedJoinSQL;
    }

    /**
         * Check Data Unique
         * @param aSysResourceProperty
         * @throws Exception
         */
    public boolean checkIsUnique(SysResourceProperty aSysResourceProperty)
        throws Exception {
        if (aSysResourceProperty == null) {
            return true;
        }

        List valueList = new ArrayList();

        List pkValueList = new ArrayList();

        pkValueList.add(aSysResourceProperty.getId());

        return isUnique(valueList.toArray(new Object[valueList.size()]),
            pkValueList.toArray(new Object[pkValueList.size()]));
    }

    /**
     * Insert Data
     * @param aSysResourceProperty
     * @throws Exception
     */
    public void insert(SysResourceProperty aSysResourceProperty)
        throws Exception {
        super.doInsert(aSysResourceProperty);
    }

    /**
         * Batch Insert Datas
         * @param aSysResourceProperty
         * @throws Exception
         */
    public void batchInsert(SysResourceProperty[] aSysResourcePropertys)
        throws Exception {
        super.doBatchInsert(aSysResourcePropertys);
    }

    /**
         * Delete Data
         * @param  final long id         *
         * @throws Exception
         */
    public void delete(final long id) throws Exception {
        super.doDeleteById(new Object[] { new Long(id) });
    }

    /**
     * Delete Datas
     * @param queryBean
     * @throws Exception
     */
    public void delete(SysResourcePropertyDAOQueryBean queryBean)
        throws Exception {
        super.doDeleteByQuery(queryBean);
    }

    public void delete(SysResourceProperty aSysResourceProperty)
        throws Exception {
        delete(aSysResourceProperty.getId());
    }

    /**
     * Update Data Selective
     * @param aSysResourceProperty
     * @throws Exception
     */
    public void update(SysResourceProperty aSysResourceProperty)
        throws Exception {
        if (aSysResourceProperty.getId() == 0) {
            throw new Exception(
                "Can not update object data whith out primary key value");
        }

        super.doUpdate(aSysResourceProperty);

        SysResourceProperty tempObj = findById(aSysResourceProperty.getId());

        if (tempObj != null) {
            PropertyUtils.copyProperties(aSysResourceProperty, tempObj);
        }
    }

    /**
     * Batch Update Datas
     * @param aSysResourcePropertys[]
     * @throws Exception
     */
    public void batchUpdate(SysResourceProperty[] aSysResourcePropertys)
        throws Exception {
        super.doBatchUpdate(aSysResourcePropertys);
    }

    /**
         * Update Data By Condictions
         * @param aSysResourceProperty
         * @param aValueCriteria
         * @param aTransaction
         * @throws Exception
         */
    public void update(SysResourceProperty aSysResourceProperty,
        SysResourcePropertyDAOQueryBean aQueryBean) throws Exception {
        super.doUpdateByQuery(aSysResourceProperty, aQueryBean);
    }

    /**
     * Update Data By Condictions
     * @param aQueryBean
     * @throws Exception
     */
    public void update(SysResourcePropertyDAOQueryBean aQueryBean)
        throws Exception {
        super.doUpdateByQuery(aQueryBean);
    }

    protected void checkNullValue(SysResourceProperty aSysResourceProperty)
        throws Exception {
        if (aSysResourceProperty.getRemarks() == null) {
            throw new Exception(
                "The SysResourceProperty.remarks value is not allow NULL");
        }

        if (aSysResourceProperty.getResourcePropertyCode() == null) {
            throw new Exception(
                "The SysResourceProperty.resourcePropertyCode value is not allow NULL");
        }

        if (aSysResourceProperty.getResourcePropertyDefaultValue() == null) {
            throw new Exception(
                "The SysResourceProperty.resourcePropertyDefaultValue value is not allow NULL");
        }

        if (aSysResourceProperty.getResourcePropertyName() == null) {
            throw new Exception(
                "The SysResourceProperty.resourcePropertyName value is not allow NULL");
        }

        if (aSysResourceProperty.getResourcePropertyOrder() == null) {
            throw new Exception(
                "The SysResourceProperty.resourcePropertyOrder value is not allow NULL");
        }

        if (aSysResourceProperty.getResourcePropertyType() == null) {
            throw new Exception(
                "The SysResourceProperty.resourcePropertyType value is not allow NULL");
        }

        if (aSysResourceProperty.getResourcePropertyValue() == null) {
            throw new Exception(
                "The SysResourceProperty.resourcePropertyValue value is not allow NULL");
        }

        if (aSysResourceProperty.getStatus() == null) {
            throw new Exception(
                "The SysResourceProperty.status value is not allow NULL");
        }

        if (aSysResourceProperty.getSysResourceId() == null) {
            throw new Exception(
                "The SysResourceProperty.sysResourceId value is not allow NULL");
        }
    }

    /**
     * Get Data By ID
     * @param  final long id         * @return  ${businessObject.name}
     * @throws Exception
     */
    public SysResourceProperty findById(final long id)
        throws Exception {
        SysResourcePropertyDAOQueryBean queryBean = new SysResourcePropertyDAOQueryBean();

        SysResourcePropertyDAOQueryBean.Criteria criteria = queryBean.createCriteria();

        criteria.andIdEqualTo(id);

        //queryBean.setDistinct(true);
        SysResourceProperty[] SysResourcePropertys = query(queryBean);

        if (SysResourcePropertys.length == 0) {
            return null;
        } else {
            return SysResourcePropertys[0];
        }
    }

    /**
         * Query Data
         * @param queryBean
         * @return SysResourceProperty[]
         * @throws Exception
         */
    public SysResourceProperty[] query(
        SysResourcePropertyDAOQueryBean queryBean) throws Exception {
        List result = super.doQuery(queryBean,
                new JdbcSysResourcePropertyRowMapper());

        return (SysResourceProperty[]) result.toArray(new SysResourceProperty[result.size()]);
    }

    /**
     * Query ${businessObject.name} Selective
     * @param queryBean
     * @return Map[]
     * @model
     * @throws Exception
     */
    public Map<String, Object>[] querySelective(
        SysResourcePropertyDAOQueryBean queryBean) throws Exception {
        if (queryBean.getSelectProperties().isEmpty()) {
            queryBean.addAllSelectProperties();
        }

        return super.doQuerySelective(queryBean);
    }

    /**
     * Query Data Count
     * @param queryBean
     * @return int
     * @throws Exception
     */
    public int queryCount(SysResourcePropertyDAOQueryBean queryBean)
        throws Exception {
        return super.doQueryCount(queryBean);
    }

    public void synchronize(SysResourceProperty[] aSysResourcePropertys,
        SysResourcePropertyDAOQueryBean queryBean) throws Exception {
        daoHelper.synchronize(aSysResourcePropertys, queryBean);
    }

    public void synchronize(SysResourceProperty[] aSysResourcePropertys,
        SysResourcePropertyDAOQueryBean queryBean,
        net.dao.IDataAccessInterface.ISynchronizeCallback<SysResourceProperty> callBack)
        throws Exception {
        daoHelper.synchronize(aSysResourcePropertys, queryBean, callBack);
    }

    public void setSysResourcePropertyOptionDao(
        com.ourteam.system.dao.ISysResourcePropertyOptionDAO SysResourcePropertyOptionDao) {
        this.SysResourcePropertyOptionDao = (com.ourteam.system.dao.impl.AbstractSysResourcePropertyOptionDAOJdbcImpl) SysResourcePropertyOptionDao;
    }

    /**
     * Query Related SysResourcePropertyOption
    * @param queryBean
     * @throws Exception
     */
    public com.ourteam.system.dao.SysResourcePropertyOption[] querySysResourcePropertyOption(
        SysResourcePropertyDAOQueryBean queryBean) throws Exception {
        StringBuffer joinSQL = new StringBuffer();

        joinSQL.append(" LEFT JOIN ").append(EntityName).append(" ON ( ");

        joinSQL.append(com.ourteam.system.dao.ISysResourcePropertyOptionDAO.SysResourcePropertyId)
               .append(" = ").append(Id);

        joinSQL.append(" ) ");

        String entityName = com.ourteam.system.dao.impl.SysResourcePropertyOptionDAO.EntityName +
            " ref_SysResourcePropertyOption";

        String[] entityFields = com.ourteam.system.dao.impl.SysResourcePropertyOptionDAO.ENTITY_FIELDS;

        String[] entityProperties = com.ourteam.system.dao.impl.SysResourcePropertyOptionDAO.ENTITY_PROPERTIES;

        List list = SysResourcePropertyOptionDao.doQuery(entityName,
                entityFields, entityProperties, queryBean, joinSQL.toString(),
                new com.ourteam.system.dao.impl.SysResourcePropertyOptionDAO.JdbcSysResourcePropertyOptionRowMapper());

        return (com.ourteam.system.dao.SysResourcePropertyOption[]) list.toArray(new com.ourteam.system.dao.SysResourcePropertyOption[list.size()]);
    }

    /**
     * Query Related SysResourcePropertyOption Selective
     * @param queryBean
     * @return Map[]
     * @model
     * @throws Exception
     */
    public Map<String, Object>[] querySysResourcePropertyOptionSelective(
        SysResourcePropertyDAOQueryBean queryBean) throws Exception {
        if (queryBean.getSelectProperties().isEmpty()) {
            queryBean.addSelectProperty(com.ourteam.system.dao.ISysResourcePropertyOptionDAO.Id,
                "id");

            queryBean.addSelectProperty(com.ourteam.system.dao.ISysResourcePropertyOptionDAO.OrderIndex,
                "orderIndex");

            queryBean.addSelectProperty(com.ourteam.system.dao.ISysResourcePropertyOptionDAO.Remarks,
                "remarks");

            queryBean.addSelectProperty(com.ourteam.system.dao.ISysResourcePropertyOptionDAO.Status,
                "status");

            queryBean.addSelectProperty(com.ourteam.system.dao.ISysResourcePropertyOptionDAO.SysResourcePropertyId,
                "sysResourcePropertyId");

            queryBean.addSelectProperty(com.ourteam.system.dao.ISysResourcePropertyOptionDAO.ValueCode,
                "valueCode");

            queryBean.addSelectProperty(com.ourteam.system.dao.ISysResourcePropertyOptionDAO.ValueLable,
                "valueLable");
        }

        StringBuffer joinSQL = new StringBuffer();

        joinSQL.append(" LEFT JOIN ").append(EntityName).append(" ON ( ");

        joinSQL.append(com.ourteam.system.dao.ISysResourcePropertyOptionDAO.SysResourcePropertyId)
               .append(" = ").append(Id);

        joinSQL.append(" ) ");

        String entityName = com.ourteam.system.dao.impl.SysResourcePropertyOptionDAO.EntityName +
            " ref_SysResourcePropertyOption";

        return SysResourcePropertyOptionDao.doQuerySelective(entityName,
            queryBean, joinSQL.toString());
    }

    /**
     * Update Related SysResourcePropertyOption
     * @param aSysResourcePropertyOption
     * @param aQueryBean
     * @model
     * @throws Exception
     */
    public void updateSysResourcePropertyOption(
        com.ourteam.system.dao.SysResourcePropertyOption aSysResourcePropertyOption,
        SysResourcePropertyDAOQueryBean aQueryBean) throws Exception {
        StringBuffer joinSQL = new StringBuffer();

        joinSQL.append(" LEFT JOIN ").append(EntityName).append(" ON ( ");

        joinSQL.append(com.ourteam.system.dao.ISysResourcePropertyOptionDAO.SysResourcePropertyId)
               .append(" = ").append(Id);

        joinSQL.append(" ) ");

        String entityName = com.ourteam.system.dao.impl.SysResourcePropertyOptionDAO.EntityName +
            " ref_SysResourcePropertyOption";

        String[] entityFields = com.ourteam.system.dao.impl.SysResourcePropertyOptionDAO.ENTITY_FIELDS;

        String[] entityProperties = com.ourteam.system.dao.impl.SysResourcePropertyOptionDAO.ENTITY_PROPERTIES;

        SysResourcePropertyOptionDao.doUpdateByQuery(entityName, entityFields,
            entityProperties, joinSQL.toString(), aSysResourcePropertyOption,
            aQueryBean);
    }

    /**
     * Delete Related SysResourcePropertyOption
     * @param aQueryBean
     * @model
     * @throws Exception
     */
    public void deleteSysResourcePropertyOption(
        SysResourcePropertyDAOQueryBean aQueryBean) throws Exception {
        StringBuffer joinSQL = new StringBuffer();

        joinSQL.append(" LEFT JOIN ").append(EntityName).append(" ON ( ");

        joinSQL.append(com.ourteam.system.dao.ISysResourcePropertyOptionDAO.SysResourcePropertyId)
               .append(" = ").append(Id);

        joinSQL.append(" ) ");

        String entityName = com.ourteam.system.dao.impl.SysResourcePropertyOptionDAO.EntityName; // + " ref_SysResourcePropertyOption";

        SysResourcePropertyOptionDao.doDeleteByQuery(entityName,
            joinSQL.toString(), aQueryBean);
    }

    public static class JdbcSysResourcePropertyRowMapper implements RowMapper<SysResourceProperty> {
        public SysResourceProperty mapRow(ResultSet resultSet, int arg1)
            throws SQLException {
            SysResourceProperty object = new SysResourceProperty();

            int columnIndex = 1;

            int __dataType;

            try {
                Object id = null;

                id = resultSet.getObject(columnIndex++);

                object.setIdNull(resultSet.getBoolean(columnIndex++));

                if (id != null) {
                    setBeanValue(object, _DataObjectProperty.Id, id);
                }

                Object remarks = null;

                remarks = resultSet.getObject(columnIndex++);

                object.setRemarksNull(resultSet.getBoolean(columnIndex++));

                if (remarks != null) {
                    setBeanValue(object, _DataObjectProperty.Remarks, remarks);
                }

                Object resourcePropertyCode = null;

                resourcePropertyCode = resultSet.getObject(columnIndex++);

                object.setResourcePropertyCodeNull(resultSet.getBoolean(
                        columnIndex++));

                if (resourcePropertyCode != null) {
                    setBeanValue(object,
                        _DataObjectProperty.ResourcePropertyCode,
                        resourcePropertyCode);
                }

                Object resourcePropertyDefaultValue = null;

                resourcePropertyDefaultValue = resultSet.getObject(columnIndex++);

                object.setResourcePropertyDefaultValueNull(resultSet.getBoolean(
                        columnIndex++));

                if (resourcePropertyDefaultValue != null) {
                    setBeanValue(object,
                        _DataObjectProperty.ResourcePropertyDefaultValue,
                        resourcePropertyDefaultValue);
                }

                Object resourcePropertyName = null;

                resourcePropertyName = resultSet.getObject(columnIndex++);

                object.setResourcePropertyNameNull(resultSet.getBoolean(
                        columnIndex++));

                if (resourcePropertyName != null) {
                    setBeanValue(object,
                        _DataObjectProperty.ResourcePropertyName,
                        resourcePropertyName);
                }

                Object resourcePropertyOrder = null;

                resourcePropertyOrder = resultSet.getObject(columnIndex++);

                object.setResourcePropertyOrderNull(resultSet.getBoolean(
                        columnIndex++));

                if (resourcePropertyOrder != null) {
                    setBeanValue(object,
                        _DataObjectProperty.ResourcePropertyOrder,
                        resourcePropertyOrder);
                }

                Object resourcePropertyType = null;

                resourcePropertyType = resultSet.getObject(columnIndex++);

                object.setResourcePropertyTypeNull(resultSet.getBoolean(
                        columnIndex++));

                if (resourcePropertyType != null) {
                    setBeanValue(object,
                        _DataObjectProperty.ResourcePropertyType,
                        resourcePropertyType);
                }

                Object resourcePropertyValue = null;

                resourcePropertyValue = resultSet.getObject(columnIndex++);

                object.setResourcePropertyValueNull(resultSet.getBoolean(
                        columnIndex++));

                if (resourcePropertyValue != null) {
                    setBeanValue(object,
                        _DataObjectProperty.ResourcePropertyValue,
                        resourcePropertyValue);
                }

                Object status = null;

                status = resultSet.getObject(columnIndex++);

                object.setStatusNull(resultSet.getBoolean(columnIndex++));

                if (status != null) {
                    setBeanValue(object, _DataObjectProperty.Status, status);
                }

                Object sysResourceId = null;

                sysResourceId = resultSet.getObject(columnIndex++);

                object.setSysResourceIdNull(resultSet.getBoolean(columnIndex++));

                if (sysResourceId != null) {
                    setBeanValue(object, _DataObjectProperty.SysResourceId,
                        sysResourceId);
                }

                return object;
            } catch (Exception e) {
                throw new SQLException(e.getMessage());
            }
        }
    }
}
